/*
 * @Author: 45F
 * @LastEditors: carol
 * @Date: 2019-05-23 15:44:10
 * @LastEditTime: 2019-11-06 09:12:43
 */

import axios from "axios";
import store from "./../store";
import Router from "@/router";
import { message } from "ant-design-vue";
import { getToken, removeToken } from "@/libs/util";

class HttpRequest {
  constructor(baseUrl) {
    this.baseUrl = baseUrl;
    this.queue = {};
  }

  getInsideConfig(param) {
    let config = {};
    if (param) {
      config = {
        baseURL: this.baseUrl,
        withCredentials: true,
        headers: {
          Accept: "application/json;charset=utf-8",
          "Content-Type": param
        }
      };
    } else {
      config = {
        baseURL: this.baseUrl,
        withCredentials: true,
        headers: {
          Accept: "application/json;charset=utf-8"
        }
      };
    }
    return config;
  }

  destroy(url) {
    delete this.queue[url];
    if (!Object.keys(this.queue).length) {
      // Spin.hide()
    }
  }

  interceptors(instance, url) {
    // 请求拦截
    instance.interceptors.request.use(
      config => {
        // 添加全局的loading...
        if (!Object.keys(this.queue).length) {
          // Spin.show() // 不建议开启，因为界面不友好
        }
        this.queue[url] = true;
        const token = getToken();
        if (token) {
          config.headers.Authorization = token;
          config.headers.TOKEN = token;
        }
        store.state.loadding = true; // 在请求发出之前进行一些操作
        return config;
      },
      error => {
        return Promise.reject(error);
      }
    );
    // 响应拦截
    instance.interceptors.response.use(
      res => {
        // debugger;
        this.destroy(url);
        store.state.loadding = false;
        try {
          const { data, status } = res;
          if (res.data.code === 401) {
           // 跳转登录页
            removeToken("kafka_token");
            Router.push({
              name: "login"
            });
          }
          return { data, status };
        } catch (e) {
          message.warning("系统繁忙");
        }
      },
      error => {
        let { status, request } = error.response;
        let res = null;
        store.state.loadding = false;
        try {
          res = JSON.parse(request.response);
        } catch (e) {
          message.warning("系统繁忙");
          return;
        }
        if (status === 401) {
          // 用户未登录
          message.error(res.message).then(() => {
            // 跳转登录页
            removeToken("token");
            Router.push({
              name: "login"
            });
          });
        } else {
          message.error(res.message || res.data);
        }
        this.destroy(url);
        return Promise.reject(error);
      }
    );
  }
  getHost(url) {
    if (sessionStorage.getItem("host")) {
      url = sessionStorage.getItem("host") + url;
    }
    return url;
  }
  request(options) {
    // console.log('server:' + options.data.server) // 获取登录填写的服务器地址，以便设置baseURL
    const instance = axios.create();
    options.url = this.getHost(options.url);
    options = Object.assign(this.getInsideConfig(options.contentType), options);
    this.interceptors(instance, options.url);
    // debugger
    return instance(options);
  }
}
export default HttpRequest;
